﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FCS;

namespace Applications
{
    public class QuickSort : BaseListFunc<int>
    {

        //quicksort [] = []
        //quicksort (x:xs) = quicksort [ i | i <- xs, i < x ] 
        //                   ++ [x] ++
        //                   quicksort [ i | i <- xs, i >= x ]
        public QuickSort(IFunList<int> list) :
            base(list, (x, xs) =>
            {
                return Compose(
                        Compose(
                            this.QuickSort(Filter(v => v < x, xs)),
                            x),
                            this.QuickSort(Filter(v => v >= x, xs)));
            })
        {
        }
    }

    public class IsContain<T> : BaseCaseFunc<bool>
    {
        /// <summary>
        /// (defun eleme (elem lista)
        ///     (cond 
        ///         ((null lista) nil)
        ///         ((equal elem (car lista)) t)
        ///         (t (eleme elem (cdr lista)))
        ///     )
        /// )
        /// </summary>
        /// <param name="val"></param>
        /// <param name="list"></param>
        public IsContain(T val, IFunList<T> list)
        {
            Case(() => IsEmpty(list), () => false);
            Case(() => Equals(val, HeadValue(list)), () => true);
            Case(() => true, () => this.IsContain(val, Tail(list)));
        }
    }
}
